package myself.study.network.udp.examples;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

/**
* @Name: UdpServer
* @Description: udp案例-聊天程序服务端：接收客户端发送的数据并进行相应的处理
* @Author: 李超	lc_jack@foxmail.com
* @CreateDate: 2017-1-12 上午9:44:08 
* @Version: V1.0
 */
public class UdpServer {
	
	public static void main(String[] args) throws IOException {
		//创建接收数据的Socket对象
		DatagramSocket server = new DatagramSocket(12345) ;
		while(true) {
			//创建接收数据包
			byte[] buf = new byte[1024] ;
			DatagramPacket packet = new DatagramPacket(buf, buf.length) ;
			//接收客户端发送过来的数据包
			server.receive(packet) ;
			//解析数据包
			String s = new String(packet.getData(), 0, packet.getData().length) ;
			//后期处理...
			System.out.println("from [" + packet.getAddress().getHostAddress() + "], data is : " + s);
		}
	}

}
